<?php
    //Office Class define
    include("ClassOffice.php");

    //Link to MySql
    include("mysql_connect.php");

// Load File
$xml = simplexml_load_file("Office.xml");
	
	// Read File
	foreach($xml->children()->children()->children() as $child)
	{	// Read Data For Each
		$OneOffice = new Office;
		$OneOffice->AddItemFromXML($child);
		//$OneOffice->PrintElement();
		
		$strSqlCommand = "SELECT * FROM REOffice ";
		$strSqlCommand .= " WHERE OfficeID = '".$OneOffice->OfficeID."' ";
		$result = mysql_query($strSqlCommand);
		if( @mysql_num_rows($result) == 0)
		{// 1. if OfficeID Not Exist => Add
			// Check Sub Table
			$strSqlCommand = "SELECT idContactInformation FROM ContactInformation ";
			$strSqlCommand .= "WHERE OfficePhone = '".$OneOffice->ContactInfo->OfficePhone."' ";
			$strSqlCommand .= "AND Fax = '".$OneOffice->ContactInfo->Fax."' ";
			$strSqlCommand .= "AND Email = '".$OneOffice->ContactInfo->Email."' ";
			$strSqlCommand .= "AND URL = '".$OneOffice->ContactInfo->URL."' ";
					
			$result = mysql_query($strSqlCommand);
			if( @mysql_num_rows($result) == 0)
			{// 1.1 if Not Exist => Add & get id
				$strSqlCommand = "INSERT INTO ContactInformation( OfficePhone, Fax, Email, URL) ";
				$strSqlCommand .= "VALUES( '".$OneOffice->ContactInfo->OfficePhone."', ";
				$strSqlCommand .= "'".$OneOffice->ContactInfo->Fax."', ";
				$strSqlCommand .= "'".$OneOffice->ContactInfo->Email."', ";
				$strSqlCommand .= "'".$OneOffice->ContactInfo->URL."')";
				
				mysql_query($strSqlCommand);
				$id = mysql_insert_id();
			}
			else
			{// 1.2 else Get idSubTable
				$rowContactInformation = @mysql_fetch_row($result);
				$id = $rowContactInformation[0];
			}
			// Add Main Table
			$strSqlCommand = "INSERT INTO REOffice( Name, ContactInformation_idContactInformation, Address, OfficeID, BrokerID, NRDSOfficeID) ";
			$strSqlCommand .= "VALUES( '".$OneOffice->Name."', ";
			$strSqlCommand .= $id.", ";
			$strSqlCommand .= "'".$OneOffice->Address."', ";
			$strSqlCommand .= "'".$OneOffice->OfficeID."', ";
			$strSqlCommand .= "'".$OneOffice->BrokerID."', ";
			$strSqlCommand .= "'".$OneOffice->NRDSOfficeID."' )";

			mysql_query($strSqlCommand);
		}
		else
		{// 2. else Check Detial
			$rowOffice = @mysql_fetch_array($result);

			// 2.1 Check Sub Table (modify or bypass)
			$strSqlCommand = "SELECT * FROM ContactInformation WHERE idContactInformation = " . $rowOffice["ContactInformation_idContactInformation"];
			$result = mysql_query($strSqlCommand);
			$rowContactInformation = @mysql_fetch_array($result);
				
			if(strcmp($rowContactInformation["OfficePhone"]	, $OneOffice->ContactInfo->OfficePhone) == 0 
			&& strcmp($rowContactInformation["Fax"] 			, $OneOffice->ContactInfo->Fax) == 0
 			&& strcmp($rowContactInformation["Email"] 		, $OneOffice->ContactInfo->Email) == 0
 			&& strcmp($rowContactInformation["URL"] 			, $OneOffice->ContactInfo->URL) == 0
				)
			{//bypass
				$bIsModify = false;
			}
			else
			{
				$strSqlCommand =  "UPDATE ContactInformation SET OfficePhone='".$OneOffice->ContactInfo->OfficePhone;
				$strSqlCommand .= "', Fax='".$OneOffice->ContactInfo->Fax;
				$strSqlCommand .= "', Email='".$OneOffice->ContactInfo->Email;
				$strSqlCommand .= "', URL='".$OneOffice->ContactInfo->URL;
				$strSqlCommand .= "' WHERE idContactInformation = ".$rowOffice["ContactInformation_idContactInformation"];
				mysql_query($strSqlCommand);
				$bIsModify = true;
			}			
			// 2.2 Check Detail (modify or bypass)	
			if(strcmp($rowOffice["Name"], $OneOffice->Name) == 0 
			&& $bIsModify == false
 			&& strcmp($rowOffice["Address"] 	, $OneOffice->Address) == 0
 			&& strcmp($rowOffice["OfficeID"] 	, $OneOffice->OfficeID) == 0
 			&& strcmp($rowOffice["BrokerID"] , $OneOffice->BrokerID) == 0
 			&& strcmp($rowOffice["NRDSOfficeID"] , $OneOffice->NRDSOfficeID) == 0
				)
			{//bypass
			}
			else
			{//idREOffice=".$rowOffice["idREOffice"];
				//$strSqlCommand .= ", 
				$strSqlCommand =  "UPDATE REOffice SET Name='".$OneOffice->Name;
				$strSqlCommand .= "', Address='".$OneOffice->Address;
				$strSqlCommand .= "', OfficeID='".$OneOffice->OfficeID;
				$strSqlCommand .= "', BrokerID='".$OneOffice->BrokerID;
				$strSqlCommand .= "', NRDSOfficeID='".$OneOffice->NRDSOfficeID;
				$strSqlCommand .= "' WHERE idREOffice = ".$rowOffice["idREOffice"];
				mysql_query($strSqlCommand);
			}
		}
	}
?>